package com.woniuxy.lrz.carlproject.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.lrz.carlproject.common.AppException;
import com.woniuxy.lrz.carlproject.common.util.BeanCopyUtil;
import com.woniuxy.lrz.carlproject.controller.form.userform.AddOrderForm;
import com.woniuxy.lrz.carlproject.controller.form.userform.AddOrderInfoForm;
import com.woniuxy.lrz.carlproject.dao.OrderInfoMapper;
import com.woniuxy.lrz.carlproject.dao.ShopcarMapper;
import com.woniuxy.lrz.carlproject.model.OrderInfo;
import com.woniuxy.lrz.carlproject.model.ShopService;
import com.woniuxy.lrz.carlproject.model.Shopcar;
import com.woniuxy.lrz.carlproject.model.UserOrder;
import com.woniuxy.lrz.carlproject.dao.UserOrderMapper;
import com.woniuxy.lrz.carlproject.service.UserOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.lrz.carlproject.service.dto.orderDto.OrderDetailDTO;
import com.woniuxy.lrz.carlproject.service.dto.orderDto.OrderGoodInfoDTO;
import com.woniuxy.lrz.carlproject.service.dto.orderDto.UserOrderListDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 用户订单表 服务实现类
 * </p>
 *
 * @author lrz
 * @since 2022-09-28
 */
@Service
@Slf4j
public class UserOrderServiceImpl extends ServiceImpl<UserOrderMapper, UserOrder> implements UserOrderService {


    @Resource
    private UserOrderMapper userOrderMapper;
    @Resource
    private OrderInfoMapper orderInfoMapper;

    @Resource
    private ShopcarMapper shopcarMapper;

    @Override
    @Cacheable(cacheNames = "shopOrder", key = "#current")
    public Page<UserOrder> getOrderByShopId(int current, int size, int shopId) {
        QueryWrapper<UserOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("shop_id",shopId);
        Page<UserOrder> page = userOrderMapper.selectPage(new Page<>(current, size), wrapper);
        if (page==null){
            throw  new AppException("未查到该店订单");
        }
        return page;
    }

    @Override
    public void updateOrderStatus(String userOrderNo,String status) {
        UpdateWrapper<UserOrder> wrapper = new UpdateWrapper<>();
        wrapper.eq("user_order_no",userOrderNo)
                .set("status",status);
        int update = userOrderMapper.update(null, wrapper);
        if (update<1){
            throw  new AppException("修改订单状态失败");
        }
    }

    //用户订单新增
    @Transactional
    public String addOrder(AddOrderForm addOrderForm) {
        Integer loginId = Integer.parseInt((String)StpUtil.getLoginId());
        UserOrder userOrder = BeanUtil.copyProperties(addOrderForm, UserOrder.class);
        userOrder.setUserOrderNo(loginId+""+ RandomUtil.randomString(16));
        userOrder.setUserId(loginId);
        userOrder.setStatus("WAITEPAY");
        int insert = userOrderMapper.insert(userOrder);
        //新增订单详情表
        List<AddOrderInfoForm> orderInfoFormList = addOrderForm.getOrderInfoFormList();
        for (AddOrderInfoForm info : orderInfoFormList) {
            OrderInfo orderInfo = BeanUtil.copyProperties(info, OrderInfo.class);
            log.info("see userOrder have correct Id : {}",userOrder);
            orderInfo.setUserOrderId(userOrder.getUserOrderId());
            int insert1 = orderInfoMapper.insert(orderInfo);
        }
        //移除购物车表、购物车详情表
        if(addOrderForm.getShopId()==0){
            for (AddOrderInfoForm info : orderInfoFormList){
                QueryWrapper<Shopcar> wrapper = new QueryWrapper<>();
                wrapper.eq("user_id",loginId);
                wrapper.eq("good_sku_id",info.getSkuId());
                int delete = shopcarMapper.delete(wrapper);
            }
        }
        //返回订单编号
        return userOrder.getUserOrderNo();
    }

    //用户取消订单  只有未支付才能取消
    public void cancelOrder(String userOrderNo) {
        QueryWrapper<UserOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("user_order_no",userOrderNo);
        UserOrder userOrder = userOrderMapper.selectOne(wrapper);
        if(userOrder==null){
            throw new AppException("该订单不存在");
        }
        if(!"WAITEPAY".equals(userOrder.getStatus())){
            throw new AppException("此订单状态无法取消订单");
        }
        int delete = userOrderMapper.delete(wrapper);
    }

    //用户签收订单  只有服务完成状态 才可以改为签收状态
    public void confirmOrder(String userOrderNo) {
        QueryWrapper<UserOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("user_order_no",userOrderNo);
        UserOrder userOrder = userOrderMapper.selectOne(wrapper);
        if(userOrder==null){
            throw new AppException("该订单不存在");
        }
        if(!"WORKED".equals(userOrder.getStatus())){
            throw new AppException("此订单状态无法签收订单");
        }
        userOrder.setStatus("DEAL");
        int i = userOrderMapper.updateById(userOrder);
    }

    //用户查看订单列表
    public List<UserOrderListDTO> getOrders() {
        Integer loginId = Integer.parseInt((String)StpUtil.getLoginId());
        QueryWrapper<UserOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",loginId);
        List<UserOrder> userOrders = userOrderMapper.selectList(wrapper);
        if(userOrders==null){
            throw new AppException("没有订单信息");
        }
        List<UserOrderListDTO> userOrderListDTOS = BeanCopyUtil.copyList(userOrders, UserOrderListDTO.class);
        return userOrderListDTOS;
    }

    //用户查看订单详情
    public OrderDetailDTO getOrderDetail(String userOrderNo) {
        QueryWrapper<UserOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("user_order_no",userOrderNo);
        UserOrder userOrder = userOrderMapper.selectOne(wrapper);
        if(userOrder==null){
            throw new AppException("该订单不存在");
        }
        OrderDetailDTO orderDetailDTO = BeanUtil.copyProperties(userOrder, OrderDetailDTO.class);
        //查询订单商品详情
        List<OrderGoodInfoDTO> goodInfoDTOS = orderInfoMapper.searchGoodDetails(userOrder.getUserOrderId());
        orderDetailDTO.setGoodInfos(goodInfoDTOS);
        return orderDetailDTO;
    }
}
